<?php
/**
 * 获取dom中的值
 * 
 * @param string $sS 匹配的字符串
 * @param string $sStartDom 开始字符串
 * @param string $sEndDom 结束字符串
 * @return array
 */
function f_regex_dom($sS, $sStartDom, $sEndDom){
	$sStartDom = str_replace('/', '\/', addslashes($sStartDom));
	$sEndDom = str_replace('/', '\/', addslashes($sEndDom));
	preg_match_all("/".$sStartDom."(.*)".$sEndDom."/iUs", $sS, $aMatch);
	return $aMatch[1];
}

/**
 * 获取dom中属性的值
 * 
 * @param string $sS 匹配的字符串
 * @param string/array $prop  属性
 * @return string / array 1.单一属性时直接返回值 2.多个属性时返回以属性为key的数组		
 */
function f_regex_dom_prop($sS, $prop){
	if (empty($sS)){
		return '';
	}
	if (is_array($prop)){
		$aR = array();
		foreach ($prop as $sProp){
			preg_match_all("/".$sProp."=\"(.*)\"/iUs", $sS, $aMatch);
			$aR[$sProp] = $aMatch[1][0];
		}
		return $aR;
	}
	preg_match_all("/".$prop."=\"(.*)\"/iUs", $sS, $aMatch);
	return $aMatch[1][0];
}

/**
 * 获取url中属性的值
 *
 * @param string $sS 匹配的字符串
 * @param string/array $prop  属性
 * @return string / array 1.单一属性时直接返回值 2.多个属性时返回以属性为key的数组
 */
function f_regex_url_prop($sS, $prop){
	if (empty($sS)){
		return '';
	}
	$aS = parse_url(html_entity_decode($sS));
	if (empty($aS['query'])){
		return '';
	}
	parse_str($aS['query'], $aPropValue);
	
	if (is_array($prop)){
		$aR = array();
		foreach ($prop as $sProp){
			$aR[$sProp] = $aPropValue[$sProp];
		}
		return $aR;
	}
	return $aPropValue[$prop];
}
/**
 * 查找字符串
 * 
 * @param string $sS 匹配的字符串
 * @param array/int $findme
 * @return array
 */
function f_regex_findStr($sS, $findme){
	if ($findme == 'int'){
		preg_match_all("/[\d]+/", $sS, $arr);
		return $arr[0];
	}
}

/**
 * 清除html中的换行回车等
 * 
 * @param string $s
 * @return string
 */
function f_regex_dom_clear($s){
	// 批量替换
	$patterns = array(
		"'<title>.*?</title>'",
		"'<meta.*?\/>'",
		"'<link.*?>'",
		"'<script.*?<\/script>'",
	);
	$replacements = array(
		"",
		"",
		"",
		"",
	);
	$s = preg_replace($patterns, $replacements, $s);
	return  str_replace(PHP_EOL, '', $s);
}

/**
 * 查找字符串内是否含有某字符串  /one时只需要存在就是true  all 必须全部存在才是true
 *
 * @param string $mystring
 * @param string/array $findme
 * @param string $sType one / all  含有一个 / 全部含有
 * @return bool
 */
function f_regex_strpos($mystring, $findme, $sType='one'){
	
	if (is_array($findme)){
		if ($sType == 'one'){
			foreach ($findme as $sFindme){
				$pos =  strpos($mystring, $sFindme);
				if ($pos !== false) {
					return true;
				}
			}
			return false;
		} else {
			foreach ($findme as $sFindme){
				$pos =  strpos($mystring, $sFindme);
				if ($pos === false) {
					return false;
				}
			}
			return true;
		}
	}
	
	$pos =  strpos($mystring, $findme);
	if ($pos !== false) {
		return true;
	}
	return false;
}

/**
 * 转码
 * 
 * @param string $s
 * @return string
 */
function f_regex_gbktoutf8($s){
	return iconv("gbk", "utf-8", $s);
}

/**
 * 输出编码
 * 
 * @return void
 */
function f_regex_debug_utf8(){
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
}

/**
 * 采集图片保存本地
 * 
 * @param string $sUrl 图片地址
 * @param string $sSavePath  保存地址
 * @return bool
 */
function f_collect_saveimg($sUrl, $sSavePath){
	ob_start();
	@readfile($sUrl);
	$sImg = ob_get_contents();
	ob_end_clean();
	$fFp2 = @fopen($sSavePath, "a");
	fwrite($fFp2,$sImg);
	fclose($fFp2);
	return is_file($sSavePath);
}

/**
 * 批量下载保存图片
 * 
 * @param array $aList 需要下载的图片
 * @param int $nSleep 每张下载后的停顿时间
 * @return array
 */
function f_collect_saveimg_batch($aList, $nSleep=5){
	if (empty($aList)){
		return false;
	}
	$aError = array();
	foreach ($aList as $aD){
		sleep($nSleep);
		$isOk = f_collect_saveimg($aD['url'], $aD['path']);
		if (!$isOk){
			$aError[] = $aD;
		}
	}
	return $aError;
}
/*
// 远程获取方法
function curl_data($url='',$data=array(),$cookie=''){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	if(!empty($data)){
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	}
	if(!empty($cookie)) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$result= curl_exec($ch);
	curl_close ($ch);
	return $result;
}
// 构造cookie
$aCookie = '';
foreach($_COOKIE as $keyC => $vC){
	$aCookie[] = $keyC.'='.urlencode($vC);
}
// 获取头部文件
$cookie = implode('; ', $aCookie);
$sHeader = curl_data('http://www.cheyian.com/index.php?m=content_1&a=header&sharefrom='.get_url(),$data=array(),$cookie);

*/
